/**
 * mixin
 * 动态json接口获取kv功能（数据优化版）
 */
import {
	apiapi as request
} from "@/api/request.js"
import {
	numMulti
} from '@/util/rocMath'

export default {
	data() {
		return {
			keyValueDatas: {},
			keyValueDatasObj: {},
			chartKeyValueDatasObj: {},
			chartKeyValueDatasArr: {},
		}
	},
	methods: {
		// 转换树形数据为扁平数据
		translateTreeToData(data) {
			const result = []
			data.forEach((item) => {
				const loop = (data) => {
					result.push({
						value: data.value,
						label: data.label,
					})
					const child = data.children
					if (child) {
						for (let i = 0; i < child.length; i++) {
							loop(child[i])
						}
					}
				}
				loop(item)
			})
			return result
		},
		// 获取字段接口数据
		async getFieldApiData(tableFdList) {
			/*
			   select
			   radio
			   checkbox
			   cascader
			   tree
			 */
			tableFdList.forEach(async (item) => {
				if (
					item.type == 'select' ||
					item.type == 'radio' ||
					item.type == 'checkbox' ||
					item.type == 'cascader' ||
					item.type == 'tree'
				) {
					let kvDatas = []
					if (item.effect.fetch.action) {
						// 请求kv数据
						let {
							data: {
								data
							}
						} = await request(item.effect.fetch.action, "GET")
						// 有 children 则转为扁平数据
						if (data.length && data[0].children && data[0].children.length) {
							data = this.translateTreeToData(data)
						}
						kvDatas = data
					} else {
						// 静态kv数据
						if (item.type == 'cascader') {
							kvDatas = item.props.options
						} else if (item.type == 'tree') {
							kvDatas = item.props.data
						} else {
							kvDatas = item.options
						}
					}
					let newKvDatasObj = {}
					if (kvDatas.length) {
						kvDatas.forEach((item) => {
							newKvDatasObj[item.value] = item.label
							// 添加样式
							newKvDatasObj[`class${item.value}`] = item.class || ''
						})
					}
					this.$set(this.keyValueDatas, item.field, kvDatas)
					this.$set(this.keyValueDatasObj, item.field, newKvDatasObj)
				}
			})
		},
		// 图表列表id显示name处理
		async getChartTableApiData(
			config_json,
			dataList,
			isIdTranName = true,
			isXishuXiaoshu = true
		) {
			const newConfigJson = JSON.parse(JSON.stringify(config_json))
			const haveApiFieldObjList = config_json.filter((fieldObj) => {
				return fieldObj.api != ''
			})
			for (let fieldObj of haveApiFieldObjList) {
				let {
					data: {
						data
					}
				} = await request(fieldObj.api, "GET")
				this.$set(this.chartKeyValueDatasArr, fieldObj.field, data)
				let newKvDatasObj = {}
				data.forEach((item) => {
					newKvDatasObj[item.value] = item.label
					this.$set(this.chartKeyValueDatasObj, fieldObj.field, newKvDatasObj)
				})
			}
			// 数据转换
			let newDataList = []
			newConfigJson.forEach((fieldObj) => {
				newDataList = dataList.map((dataItem) => {
					// id name 转换
					if (isIdTranName) {
						if (fieldObj.api != '') {
							dataItem[fieldObj.field] =
								this.chartKeyValueDatasObj[fieldObj.field][
									dataItem[fieldObj.field]
								]
						}
					}
					if (isXishuXiaoshu) {
						// 系数计算转换
						if (
							dataItem[fieldObj.field] != null &&
							fieldObj.ratio != '' &&
							!isNaN(Number(fieldObj.ratio))
						) {
							dataItem[fieldObj.field] = numMulti(
								dataItem[fieldObj.field],
								fieldObj.ratio
							)
						}
						// 小数位处理
						if (fieldObj.bit != '' && !isNaN(fieldObj.bit)) {
							if (!isNaN(Number(dataItem[fieldObj.field]))) {
								dataItem[fieldObj.field] = Number(
									dataItem[fieldObj.field]
								).toFixed(Number(fieldObj.bit))
							}
						}
					}
					return dataItem
				})
			})
			return newDataList
		},
	},
}
